﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang_CT_ChonPhieu : DevExpress.XtraEditors.XtraForm
    {
        string ma = "";
        string BH_PHIEU_MA = "";
        string BH_PHIEU_MA2 = "";
        DataTable tbDeleteLeft = null;
        DataTable tbDeleteRight = null;
        #region "   mh_banhang_CT_ChonPhieu"
        public mh_banhang_CT_ChonPhieu(string _DM_BAN_MA)
        {
            InitializeComponent();
            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            // -== -== -== -== -== -== -== -== -==  DM_KHUVUC2
            // -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -== -==
            string sSQL = "SELECT * FROM BH_PHIEU_TMP WHERE DM_BAN_MA LIKE '" + _DM_BAN_MA + "' ORDER BY OBJID";
            thuvien othuvien = new thuvien();
            DataTable tbPHIEU = othuvien.SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            DataTable tbPHIEUCOPY = tbPHIEU.Copy();
            DataRow drcopy = tbPHIEUCOPY.NewRow();
            //drcopy["OBJID"] = " -- Phiếu mới--";
            //drcopy["TONG_TIEN"] = 0;
            //tbPHIEUCOPY.Rows.InsertAt(drcopy, 0);

            othuvien.f_showdataoncombox(cboPhieuLeft, tbPHIEU, "OBJID", "OBJID");
            othuvien.f_showdataoncombox(cboPhieuRight, tbPHIEUCOPY, "OBJID", "OBJID");
            
            //PHIEU_A
            if (tbPHIEU.Rows.Count > 0)
                BH_PHIEU_MA = tbPHIEU.Rows[0]["OBJID"] + "";
            
            //PHIEU_B
            if (tbPHIEUCOPY.Rows.Count > 1)//> 2 PHIEU
                BH_PHIEU_MA2 = tbPHIEUCOPY.Rows[1]["OBJID"] + "";
            else 
                BH_PHIEU_MA2 = tbPHIEUCOPY.Rows[0]["OBJID"] + "";

            bLoadForm = true;
        }
        #endregion

        #region "   ShowData_tbLeft"
        void ShowData_tbLeft()
        {
            string sSQL = "SELECT a.OBJID, a.MAHH, a.SO_LUONG, a.DON_GIA, a.CHIETKHAU_PHANTRAM, a.CHIETKHAU_TIEN, a.VAT_PHANTRAM, a.VAT_TIEN, a.THANH_TIEN, " +
                          "     OBJNAME = (CASE WHEN A.MAHH LIKE 'Tien_Gio' THEN N'Tiền Giờ' ELSE b.OBJNAME END), a.GHI_CHU, a.NGAY_TINH_GIO " +
                          "FROM BH_PHIEU_CT_TMP a " +
                          "     LEFT JOIN DM_HH b ON a.MAHH = b.OBJID " +
                          "WHERE BH_PHIEU_MA LIKE '" + BH_PHIEU_MA + "' "
                          ;

            DataTable tbMain1 = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlLeft.DataSource = tbMain1;
        }
        #endregion

        #region "   ShowData_tbRight"
        void ShowData_tbRight()
        {
            string sSQL = "SELECT a.OBJID, a.MAHH, a.SO_LUONG, a.DON_GIA, a.CHIETKHAU_PHANTRAM, a.CHIETKHAU_TIEN, a.VAT_PHANTRAM, a.VAT_TIEN, a.THANH_TIEN, " +
                          "     OBJNAME = (CASE WHEN A.MAHH LIKE 'Tien_Gio' THEN N'Tiền Giờ' ELSE b.OBJNAME END), a.GHI_CHU, a.NGAY_TINH_GIO " +
                          "FROM BH_PHIEU_CT_TMP a " +
                          "     LEFT JOIN DM_HH b ON a.MAHH = b.OBJID " +
                          "WHERE BH_PHIEU_MA LIKE '" + BH_PHIEU_MA2 + "' "
                          ;
            DataTable tbMain2 = new thuvien().SP_SelectDataTable("sp_executesql", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControlRight.DataSource = tbMain2;
        }
        #endregion

        #region "   mh_banhang1_TachGopBan_Load"
        Boolean bLoadForm = false;
        private void mh_banhang1_TachGopBan_Load(object sender, EventArgs e)
        {
            cboPhieuLeft._SetselectedID(BH_PHIEU_MA);
            cboPhieuRight._SetselectedID(BH_PHIEU_MA2);
        }
        #endregion

        #region "   btnThoat_Click"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
        #endregion

       
        #region "   cboPhieuLeft_SelectedIndexChanged"
        private void cboPhieuLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bLoadForm)
            {
                bLoadForm = false;
                BH_PHIEU_MA = cboPhieuLeft._GetselectedID();
                this.ShowData_tbLeft();
                bLoadForm = true;
            }
        }
        #endregion

        #region "   cboPhieuRight_SelectedIndexChanged"
        private void cboPhieuRight_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bLoadForm)
            {
                bLoadForm = false;
                BH_PHIEU_MA2 = cboPhieuRight._GetselectedID();
                if (BH_PHIEU_MA2.IndexOf("--") >= 0)
                { // them PHIEU moi
                    DataTable tbPhieu = ((DataTable)cboPhieuRight.Datasource).Copy();
                    //9999

                }
                this.ShowData_tbRight();
                bLoadForm = true;                
            }
        }
        #endregion

        
        #region "   TachGop"
        void TachGop(DataTable tbDataRemove, DataTable tbDataAdd, DataRow dr, string strDEL_LR, float SO_LUONG_Tru)
        {
            string OBJID = dr["OBJID"] + "";
            string MAHH = dr["MAHH"] + "";
            if (MAHH != "")
            {
                string Mon = dr["OBJNAME"] + "";
                float SO_LUONG = 0;                
                ///////////////////////Add
                if (tbDataAdd.Select("MAHH LIKE '" + MAHH + "'").Length == 0)
                {//Add.New                    
                    DataRow drAddTmp = tbDataAdd.NewRow();
                    drAddTmp.ItemArray = dr.ItemArray;
                    drAddTmp["SO_LUONG"] = SO_LUONG_Tru;
                    tbDataAdd.Rows.Add(drAddTmp);
                }
                else
                {//Add.Exists
                    DataRow drAddTmp = (DataRow)tbDataAdd.Select("MAHH LIKE '" + MAHH + "'")[0];
                    float.TryParse(drAddTmp["SO_LUONG"] + "", out SO_LUONG);
                    drAddTmp["SO_LUONG"] = SO_LUONG + SO_LUONG_Tru;
                }

                ///////////////////////Remove
                foreach (DataRow drDel in tbDataRemove.Rows)
                    if (drDel["OBJID"] + "" == OBJID)
                    {
                        float.TryParse(drDel["SO_LUONG"] + "", out SO_LUONG);
                        if (SO_LUONG <= SO_LUONG_Tru)
                        {
                            if (strDEL_LR.IndexOf("R")>=0)
                            {
                                if (tbDeleteRight == null)
                                    tbDeleteRight = tbDataAdd.Clone();
                                DataRow drTmp = tbDeleteRight.NewRow();
                                drTmp.ItemArray = drDel.ItemArray;
                                tbDeleteRight.Rows.Add(drTmp);
                            }
                            else
                            {
                                if (tbDeleteLeft == null)
                                    tbDeleteLeft = tbDataAdd.Clone();
                                DataRow drTmp = tbDeleteLeft.NewRow();
                                drTmp.ItemArray = drDel.ItemArray;
                                tbDeleteLeft.Rows.Add(drTmp);
                            }
                            tbDataRemove.Rows.Remove(drDel);
                        }
                        else
                        {
                            float SLTmp = SO_LUONG - SO_LUONG_Tru;
                            SLTmp = (float)(Math.Round((double)SLTmp, 2));
                            drDel["SO_LUONG"] = SLTmp;
                        }
                        break;
                    }
                gridControlLeft.Refresh();
                gridControlRight.Refresh();
            }
        }
        #endregion

        #region "   TinhTongTien"
        void RefreshTable()
        {
            //////////////////
            this.TinhTongTien((DataTable)gridControlLeft.DataSource);
            this.TinhTongTien((DataTable)gridControlLeft.DataSource);

            //////////////////            

            if (BH_PHIEU_MA2.IndexOf("--") >= 0)
            {
                DataRow drLeft = null;
                DataRow drRight = null;
                if (((DataTable)cboPhieuLeft.Datasource).Select("OBJID LIKE '" + BH_PHIEU_MA + "'").Length > 0)
                    drLeft = ((DataTable)cboPhieuLeft.Datasource).Select("OBJID LIKE '" + BH_PHIEU_MA + "'")[0];

                if (((DataTable)cboPhieuRight.Datasource).Select("OBJID LIKE '" + BH_PHIEU_MA2 + "'").Length > 0)
                    drRight = ((DataTable)cboPhieuRight.Datasource).Select("OBJID LIKE '" + BH_PHIEU_MA2 + "'")[0];
                if (drLeft != null && drRight != null)
                { //Phieu.New()
                    drRight.ItemArray = drLeft.ItemArray;
                    drRight["OBJID"] = BH_PHIEU_MA2;
                }
            }
        }

        void TinhTongTien(DataTable _tbBH_PHIEU_CT_TMP)
        {
            float iSO_LUONG, iDON_GIA, iCHIETKHAU_TIEN, iVAT_TIEN;
            foreach (DataRow dr in _tbBH_PHIEU_CT_TMP.Select("MAHH NOT LIKE 'Tien_Gio'"))
            {
                if (dr["MAHH"] + "" != "Tien_Gio")
                {
                    float.TryParse(dr["SO_LUONG"] + "", out iSO_LUONG);
                    float.TryParse(dr["DON_GIA"] + "", out iDON_GIA);
                    float.TryParse(dr["CHIETKHAU_TIEN"] + "", out iCHIETKHAU_TIEN);
                    float.TryParse(dr["VAT_TIEN"] + "", out iVAT_TIEN);
                    dr["THANH_TIEN"] = iSO_LUONG * iDON_GIA - iCHIETKHAU_TIEN + iVAT_TIEN;
                }
                //break;
            }
        }
        #endregion

        public string ShowForm()
        {
            this.ShowDialog();
            return ma;
        }

        private void btnChon_Left_Click(object sender, EventArgs e)
        {
            if (BH_PHIEU_MA != "")
            {
                this.DialogResult = DialogResult.OK;
                ma = BH_PHIEU_MA;
                this.Close();
            }
        }

        private void btnChon_Right_Click(object sender, EventArgs e)
        {
            if (BH_PHIEU_MA2 != "")
            {
                this.DialogResult = DialogResult.OK;
                ma = BH_PHIEU_MA2;
                this.Close();
            }
        }

        private void bton_thoat_1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}